/*
 * 制造商edit组件
 * @Author: zfx
 * @Date: 2018-06-19 17:53:27
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>
    .ivu-form-item-content {
        img {
            width: 150px;
        }
    }
</style>
<template>
	<div class="v-customer-banner-edit">
		<Form
			ref="formValidate"
			:model="formValidate"
			:rules="ruleValidate"
			:label-width="labelWidth(120, 120)"
			name="create__edit">

			<Form-item
				:label="$t('manufacturer.name')"
				prop="name">
				<i-input
					v-model="formValidate.name"
					:placeholder="$t('manufacturer.please_select_name')"
					name="create-name"
					type="text"/>
			</Form-item>

			<!-- 联系人 -->
			<Form-item
				:label="$t('manufacturer.contact')"
				prop="contact">
				<i-input
					v-model="formValidate.contact"
					:placeholder="$t('manufacturer.please_select_contact')"
					name="create-contact"
					type="text"/>
			</Form-item>

			<!-- 电话-->
			<Form-item
				:label="$t('manufacturer.telphone')"
				prop="telphone">
				<i-input
					v-model="formValidate.telphone"
					:placeholder="$t('manufacturer.please_select_telphone')"
					name="create-telphone"
					type="text"

				/>
			</Form-item>

			<!-- 地址 -->
			<Form-item
				:label="$t('manufacturer.address')"
				prop="address">
				<i-input
					v-model="formValidate.address"
					:placeholder="$t('manufacturer.please_select_address')"
					name="create-address"
					type="text"/>
			</Form-item>


			<Form-item>
				<Button
					type="primary"
					name="create__submit"
					@click="handleSubmit('formValidate')">
					{{ $t('common.update') }}
				</Button>
			</Form-item>
		</Form>
	</div>
</template>

<script>

import labelWidthMixin from 'src/views/mixins/labelWidthMixin';
import { validateUpdateImage } from 'src/validate'; // 引入校验文件
import Uploader from 'src/views/component/Uploader';

export default {

	name: 'ManufacturerEdit',
	components: {
		Uploader,
	},
	mixins: [labelWidthMixin],
	props: {
		// name: {
		// 	type: String,
		// 	default() {
		// 		return {};
		// 	},
		// },
		// contact: {
		// 	type: String,
		// 	default() {
		// 		return [];
		// 	},
		// },
		// telphone: {
		// 	type: String,
		// 	default() {
		// 		return [];
		// 	},
		// },
		// address: {
		// 	type: String,
		// 	default() {
		// 		return [];
		// 	},
		// },

		loading: {
			type: Boolean,
			default() {
				return false;
			},
		},
		manufacturer_solo: {
			type: Object,
			default() {
				return {};
			},
		},
	},

	data() {
		return {

			formValidate: {
				name: '', // 厂商

			 contact: '', // 联系人

			 telphone: '', // 电话

			 address: '', // 地址
			},

			ruleValidate: { // 表单验证规则
				name: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('manufacturer.name') }),
						trigger: 'blur,change',
					},
				],

				contact: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('manufacturer.contact') }),
						trigger: 'blur,change',
					},
				],

				telphone: [
					{

						required: true,
						message: this.$t('validate.required', { field: this.$t('manufacturer.telphone') }),
						trigger: 'blur,change',
					},
				],

				address: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('manufacturer.address') }),
						trigger: 'blur,change',
					},
				],

			},
		};
	},
	created() {
		this.initFormData();
	},
	methods: {
		initFormData() {
			if (this.manufacturer_solo) {
				// console.log(this.manufacturer_solo);

				this.formValidate.contact = this.manufacturer_solo.contact || '';

				this.formValidate.telphone = this.manufacturer_solo.telphone;

				this.formValidate.name = this.manufacturer_solo.name;

				this.formValidate.address = this.manufacturer_solo.address;
			}
		},


		/**
         * 更新Banner信息
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleSubmit(name) {
			if (this.loading) return;
			this.$refs[name].validate((valid) => {
				if (valid) {
					this.$emit('update', this.manufacturer_solo.id, this.formDataCreate());
				} else {
					this.$Message.error(this.$t('common.exist_empty_field'));
				}
			});
		},

		/**
         * 手动拼接表单数据
         * @return {Object} {拼接好的表单数据}
         */
		formDataCreate() {
			const new_object = {
				_method: 'PUT',

				name: this.formValidate.name,

				contact: this.formValidate.contact,

				telphone: this.formValidate.telphone,

				address: this.formValidate.address,
			};
			const form_data = new FormData();

			Object.keys(new_object).forEach((key) => {
				switch (key) {
				case 'values':

					new_object[key].forEach((tag, index) => {
						form_data.append(`${key}[${index}]`, tag);
					});

					break;

				default:

					form_data.append(key, new_object[key]);

					break;
				}
			});
			return form_data;
		},

	},
};
</script>
